package com.gem.service.impl;

import com.gem.domain.User;
import com.gem.mapper.UserMapper;
import com.gem.service.UserSystemService;
import com.gem.util.Md5Util;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Bravea❤ ·勇
 * Description: 人员维护service
 * create
 * Modified By:
 */
@Service
@Transactional
public class UserSystemServiceImpl implements UserSystemService {

    @Autowired
    private UserMapper mapper;
    @Override
    public boolean addUser(User u) {
        try {
            u.setPassword(Md5Util.encodeByMd5(u.getPassword()));
            return mapper.save(u) > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean deleteUserById(Integer[] uid) {
        try {
            boolean isDeleteSuccess = true;
            for (Integer integer : uid) {
                if(integer != null){
                    if(mapper.deleteById(integer)<=0){
                        isDeleteSuccess = false;
                        break;
                    }
                }
            }
            if(isDeleteSuccess){
                return true;
            }else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Override
    public boolean modifyUser(User u) {
        try {
            u.setPassword(Md5Util.encodeByMd5(u.getPassword()));
            return mapper.modifyUser(u);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public User findUserById(Integer uid) {
        try {
            return mapper.findById(uid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public PageInfo<User> findUserPageWidtM(int currentPage, int pageSize) {
        try {
            PageHelper.startPage(currentPage, pageSize);
            PageInfo<User> pageInfo  = new PageInfo<>(mapper.findMmembers());
            return pageInfo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public PageInfo<User> findUserPageWidtHrc(int currentPage, int pageSize) {
        try {
            PageHelper.startPage(currentPage, pageSize);
            PageInfo<User> pageInfo  = new PageInfo<>(mapper.findHrc());
            return pageInfo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public PageInfo<User> findUserPageWidtCompany(int currentPage, int pageSize) {
        try {
            PageHelper.startPage(currentPage, pageSize);
            PageInfo<User> pageInfo  = new PageInfo<>(mapper.findAllEmembers());
            return pageInfo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public PageInfo<User> findUserPageWidtJober(int currentPage, int pageSize) {
        try {
            PageHelper.startPage(currentPage, pageSize);
            PageInfo<User> pageInfo  = new PageInfo<>(mapper.findAllJmembers());
            return pageInfo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public PageInfo<User> findAllUserPage(int currentPage, int pageSize) {
        try {
            PageHelper.startPage(currentPage, pageSize);
            PageInfo<User> pageInfo  = new PageInfo<>(mapper.findAllUser());
            return pageInfo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<User> findAllUser() {
        try {
            return mapper.findAllUser();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public PageInfo<User> findUserWithPageByName(int currentPage, int pageSize, String name) {
        try {
            name = "%" + name + "%";
            PageHelper.startPage(currentPage, pageSize);
            PageInfo<User> pageInfo  = new PageInfo<>(mapper.findUserByName(name));
            return  pageInfo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }



}
